import HttpService from '../../service/HttpService'
import ApiUrl from '../../service/ApiUrl'
import Vue from 'vue'
// import { deepCopy } from '../../service/Util'
import Util from '../../service/Util'
import { Cart } from '../../service/Storage'

let loading = Util.loading();
// mutations-type config
const Types = {
	GET_ALL_CATEGORY: 'GET_ALL_CATEGORY',
	GET_ALL_CATEGORY_SUCCESS: 'GET_ALL_CATEGORY_SUCCESS',
	GET_ALL_CATEGORY_FAILED: 'GET_ALL_CATEGORY_FAILED',
	GET_CATEGORY_ITEM: 'GET_CATEGORY_ITEM',
	GET_CATEGORY_ITEM_SUCCESS: 'GET_CATEGORY_ITEM_SUCCESS',
	GET_CATEGORY_ITEM_FAILED: 'GET_CATEGORY_ITEM_FAILED',
	ADD_CATEGORY_ITEM_TO_CART: 'ADD_CATEGORY_ITEM_TO_CART'
}

// init state
const state = {
    allCategory: [],
    categoryItems: []
}

const getters = {
    allCategory: state => state.allCategory,
    categoryItems: state => state.categoryItems
}

// action
const actions = {
	getAllCategory ({commit, state}){
		commit(Types.GET_ALL_CATEGORY)
		let opts = {
			apiObj : ApiUrl.getGoodsCategory
		};
		HttpService.accessAPI(opts, result => { 
			commit( Types.GET_ALL_CATEGORY_SUCCESS, {result})
		}, reason => { commit(Types.GET_ALL_CATEGORY_FAILED, { reason }) });
	},
	getCategoryItems ({commit, state}, categoryId){
		commit(Types.GET_CATEGORY_ITEM)
		let opts = {
			apiObj : ApiUrl.getGoodsItems,
			query:{
				category_id: categoryId
			}
		};
		HttpService.accessAPI( opts, result => { 
			commit( Types.GET_CATEGORY_ITEM_SUCCESS, {result, categoryId})
		}, reason => { commit(Types.GET_CATEGORY_ITEM_FAILED, { reason }) });
	}

}

// init mutations 
const mutations = {
	[Types.GET_ALL_CATEGORY] ( state ){
		state.allCategory = [];
		loading.start();
	},
	[Types.GET_ALL_CATEGORY_SUCCESS] ( state, { result } ){
		console.log( 'get all category success', result );
	    result.map(function(value){
		    if( value.icon ){
		    	value.icon += '?imageView2/2/w/700';
		    }else{
		    	value.icon = '/static/placeholder.png';
		    }
	    })
		state.allCategory = result;
		loading.close();
	},
	[Types.GET_ALL_CATEGORY_FAILED] ( state, { result } ){
		state.allCategory = [];
		loading.close();
		console.log( 'mutations failed', result );
	},
	[Types.GET_CATEGORY_ITEM] ( state ){
		state.categoryItems = [];
		loading.start();
	},
	[Types.GET_CATEGORY_ITEM_SUCCESS] ( state, { result, categoryId } ){
		console.log( 'get category items success', result );
		let carts = Cart.get();
	    result.map(function(value){
	    	value.selected = false;
	    	carts.map(function(v){
	    		if( v.id == value.id ){
	    			value.selected = true;
	    		}
	    	})
		    if( value.icon ){
		    	value.icon += '?imageView2/2/w/700';
		    }else{
		    	value.icon = '/static/placeholder.png';
		    }
		    if( value.cover ){
		    	value.cover += '?imageView2/2/w/700';
		    }else{
		    	value.cover = '/static/placeholder.png';
		    }
	    })
		state.categoryItems = result;
		state.allCategory.map(function(value){
			value.active = false;
			if( value.id == categoryId ){
				value.active = true;
			}
		})
		loading.close();
	},
	[Types.GET_CATEGORY_ITEM_FAILED] ( state, { result } ){
		state.categoryItems = [];
		loading.close();
		console.log( 'mutations failed', result );
	}
}

export default {
  state,
  getters,
  actions,
  mutations
}